<?php

namespace App\Model;

use Illuminate\Http\Request;

class ActiveCertify extends BaseModel
{
    protected $connection = 'mysql';
    protected $table = 'active_certify';
    protected $fillable = [ 'type', 'card_name', 'card_id', 'img', 'user_id' ];
    
    public function getImgAttribute($value){
        return json_decode($value, true);
        if(!$value) return '';
        $value = json_decode($value, true);
        return array_map(function ($item){
            return $this->getOSSUrl().$item;
        }, $value);
    }
    
    public function createData(Request $request){
        return self::query()->updateOrcreate(['user_id' => $request->input('user_id')],array_merge(
            $request->only(['type', 'card_name', 'img','card_id']),
            [
                'img' => json_encode($request->input('img'))
            ]));
    }
    
    public function getInfo($user_id){
        return self::find($user_id);
    }
}
